/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aal.business;

import com.aal.bean.EmployeeFacadeLocal;
import com.aal.bean.WarehouseFacade;
import com.aal.bean.WarehouseIOFacadeLocal;
import com.aal.entity.Employee;
import com.aal.entity.WarehouseIO;
import java.util.Collection;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
/**
 *
 * @author quy
 */
@Stateless
public class EmployeeManagement implements EmployeeManagementLocal {
    @EJB
    private WarehouseIOFacadeLocal warehouseIOFacade;

    @EJB
    private EmployeeFacadeLocal employeeFacade;

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @Override
    @Interceptors(PasswordInterceptor.class)
    public boolean createUser(String userName, String password, String firstName, String lastName, String email, String phone, boolean gender) {
        try {
            Employee emp = new Employee();
            emp.setUsername(userName);
            emp.setPassword(password);
            emp.setFirstname(firstName);
            emp.setLastname(lastName);
            emp.setEmail(email);
            emp.setPhone(phone);
            emp.setGender(gender);
            employeeFacade.create(emp);
        } catch (Exception ex) {
            return false;
        }
        return true;
    }
    @Override
    @Interceptors(PasswordInterceptor.class)
    public boolean editUser(String username, String password, String firstname, String lastname, String email, String phone, boolean gender) {
        try{
        Employee emp = employeeFacade.find(username);
        if (password != null) {
            emp.setPassword(password);
        }
        if (firstname != null) {
            emp.setFirstname(firstname);
        }
        if (lastname != null) {
            emp.setLastname(lastname);
        }
        if (email != null) {
            emp.setEmail(email);
        }
        if (phone != null) {
            emp.setPhone(phone);
        }
        emp.setGender(gender);
        employeeFacade.edit(emp);
        }
        catch(Exception ex){
            return false;
        }
        return true;
    }
    @Interceptors(PasswordInterceptor.class)
    @Override
    public boolean loginOk(String username,String password){
        try{
            Employee employee = employeeFacade.loginOk(username, password);
            return true;
        }catch(Exception ex){
            return false;
        }
    }
    @Override
    public List<Employee> searchEmployee(String username) {
        return employeeFacade.findUser(username);
    }
}
